<?php

namespace App\Http\Controllers;

use App\Modules\User\Models\Admin;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;
use Kris\LaravelFormBuilder\FormBuilderTrait;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Facades\Gate;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests, FormBuilderTrait;

    public $authUser;

    public function checkAuth($merchantAuth, $auth)
    {
        $this->authUser= Auth::user();
        if ($this->authUser->is_merchant_staff == Admin::MERCHANT_AUTH) {
            if (gettype($merchantAuth) != "string") {
                $this->authorizeAny($merchantAuth);
            } else {
                $this->authorize($merchantAuth);
            }
        } else {
            if (gettype($auth) != "string") {
                $this->authorizeAny($auth);
            } else {
                $this->authorize($auth);
            }
        }
    }

    public function authorizeAny($abilities)
    {
        if (!Gate::any($abilities)) {
            throw new AuthorizationException();
        }
    }
}
